/* 
 * ***** BEGIN LICENSE BLOCK ***** Version: NPL 1.1/GPL 2.0/LGPL 2.1 The
 * contents of this file are subject to the Netscape Public License Version 1.1
 * (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at http://www.mozilla.org/NPL/
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License. The
 * Original Code is lutsko.com code. The Initial Developer of the Original Code
 * is James Lutsko. Portions created by the Initial Developer are Copyright (C)
 * 2003 the Initial Developer. All Rights Reserved. Contributor(s):
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or the
 * GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which
 * case the provisions of the GPL or the LGPL are applicable instead of those
 * above. If you wish to allow use of your version of this file only under the
 * terms of either the GPL or the LGPL, and not to allow others to use your
 * version of this file under the terms of the NPL, indicate your decision by
 * deleting the provisions above and replace them with the notice and other
 * provisions required by the GPL or the LGPL. If you do not delete the
 * provisions above, a recipient may use your version of this file under the
 * terms of any one of the NPL, the GPL or the LGPL. ***** END LICENSE BLOCK
 * *****
 */

package autoteam.tables;

import java.util.Vector;

import autoteam.Child;
import autoteam.ChildPair;
import autoteam.League;

public class ChildPairTableModel extends AutoTeamTableModel
{
    private static final long serialVersionUID = 2619975773583913934L;

    Vector data = null;
    boolean doSibs = true;
    League doc = null;
    
    public ChildPairTableModel( League _doc, boolean _doSibs )
    {
	doc = _doc;
	doSibs = _doSibs;
	if ( doSibs )
	    data = doc.sibs;
	else
	    data = doc.carPools;
	
    }
    
    public int getColumnCount( )
    {
	return (doSibs ? 2 : 4);
    }
    
    public int getRowCount( )
    {
	return data.size( );
    }
    
    public void add( int j )
    {
	data.add( new ChildPair( null, null ) );
    }
    
    public void delete( int r )
    {
	data.remove( r );
    }
    
    public String getColumnName( int col )
    {
	if ( col == 0 )
	    return "Child";
	if ( col == 1 )
	    return (doSibs ? "Sibling" : "Carpool with");
	if ( col == 2 )
	    return "Active";
	return "Comments";
    }
    
    public Object getValueAt( int row, int col )
    {
	if ( row >= data.size( ) )
	    return null;
	if ( col == 0 )
	    return ((ChildPair) data.get( row )).child1;
	if ( col == 1 )
	    return ((ChildPair) data.get( row )).child2;
	if ( col == 2 )
	    return (((ChildPair) data.get( row )).active ? Boolean.TRUE : Boolean.FALSE);
	Child c = (Child) ((ChildPair) data.get( row )).child1;
	if ( c == null )
	    return null;
	return c.comment;
    }
    
    /*
     * JTable uses this method to determine the default renderer/ editor for
     * each cell. If we didn't implement this method, then the last column would
     * contain text ("true"/"false"), rather than a check box.
     */
    public Class getColumnClass( int c )
    {
	if ( c == 3 )
	    return getColumnName( 3 ).getClass( );
	if ( c == 2 )
	    return Boolean.TRUE.getClass( );
	return Child.class;
    }
    
    /*
     * Don't need to implement this method unless your table's editable.
     */
    public boolean isCellEditable( int row, int col )
    {
	return (col < 3);
    }
    
    public void setValueAt( Object value, int row, int col )
    {
	if ( row >= data.size( ) )
	    return;
	if ( col > 2 )
	    return;
	if ( col == 2 )
	{
	    ((ChildPair) data.get( row )).active = (value == Boolean.TRUE);
	    return;
	}
	
	Child c1 = (Child) ((ChildPair) data.get( row )).child1;
	Child c2 = (Child) ((ChildPair) data.get( row )).child2;
	
	if ( col == 0 )
	{
	    c1 = (Child) value;
	    ((ChildPair) data.get( row )).child1 = c1;
	}
	else
	{
	    c2 = (Child) value;
	    ((ChildPair) data.get( row )).child2 = c2;
	}
	
	// this makes sure that the comments column gets updated
	// whenever the first child changes
	if ( !doSibs && col == 0 )
	    fireTableDataChanged( );
	
    }
    
    private void printDebugData( )
    {
	int numRows = getRowCount( );
	int numCols = getColumnCount( );
	
	/*
	 * for (int i=0; i < numRows; i++) { System.out.print("    row " + i +
	 * ":"); for (int j=0; j < numCols; j++) { System.out.print("  " +
	 * data[i][j]); } System.out.println(); }
	 */
	System.out.println( "--------------------------" );
    }
}
